package io.renren.hd.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.renren.config.MongoManager;
import io.renren.dao.GeneratorDao;
import io.renren.hd.dto.DataSourceDTO;
import io.renren.hd.pojo.GenConfig;
import io.renren.hd.pojo.GenDatasource;
import io.renren.hd.service.ApiDataSourceService;
import io.renren.hd.service.GenCodeService;
import io.renren.hd.service.GenDatasourceService;
import io.renren.utils.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author liuzhen
 * @date 2022/4/23 13:58
 * @Description:
 */
@Service
public class GenCodeServiceImpl implements GenCodeService {

    @Autowired
    private GeneratorDao generatorDao;

    @Autowired
    private ApiDataSourceService apiDataSourceService;

    @Autowired
    private GenDatasourceService genDatasourceService;

    @Override
    public byte[] generatorCode(List<GenConfig> configList) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        for (GenConfig genConfig : configList) {
            String tableName = genConfig.getTableName();
            String datasourceId = genConfig.getDatasourceId();

            GenDatasource datasource = genDatasourceService.getById(datasourceId);
            if (ObjectUtils.isEmpty(datasource)){
                throw new RuntimeException("没找到配置数据源"+datasource.getId());
            }
            DataSourceDTO dataSourceDTO = getDataSourceDTO(datasource);
            //添加数据源
            apiDataSourceService.add(dataSourceDTO);
            //手动切换
            DynamicDataSourceContextHolder.push(datasource.getName());

            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);

            GenUtils.generatorCode(table, columns, zip,genConfig);

        }

        //代码生成完切换数据源为主节点
        DynamicDataSourceContextHolder.push("master");
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    @Override
    public Map<String, Object> generatorCodeView(GenConfig genConfig) {

        String tableName = genConfig.getTableName();
        String datasourceId = genConfig.getDatasourceId();

        GenDatasource datasource = genDatasourceService.getById(datasourceId);
        if (ObjectUtils.isEmpty(datasource)){
            throw new RuntimeException("没找到配置数据源"+datasource.getId());
        }
        DataSourceDTO dataSourceDTO = getDataSourceDTO(datasource);
        //添加数据源
        apiDataSourceService.add(dataSourceDTO);
        //手动切换
        DynamicDataSourceContextHolder.push(datasource.getName());

        //查询表信息
        Map<String, String> table = queryTable(tableName);
        //查询列信息
        List<Map<String, String>> columns = queryColumns(tableName);
        DynamicDataSourceContextHolder.push("master");
        return GenUtils.generatorCodeView(table, columns,genConfig);
    }

    private DataSourceDTO getDataSourceDTO(GenDatasource datasource) {
        DataSourceDTO dataSourceDTO = new DataSourceDTO();
        dataSourceDTO.setPoolName(datasource.getName());
        dataSourceDTO.setDriverClassName(datasource.getDriverClass());
        dataSourceDTO.setUrl(datasource.getUrl());
        dataSourceDTO.setUsername(datasource.getUsername());
        dataSourceDTO.setPassword(datasource.getPassword());
        return dataSourceDTO;
    }


    public Map<String, String> queryTable(String tableName) {
        return generatorDao.queryTable(tableName);
    }

    public List<Map<String, String>> queryColumns(String tableName) {
        return generatorDao.queryColumns(tableName);
    }

}
